// GraphicsFullScreenVideo.cpp : implementation file
//

#include "stdafx.h"
#include "MultiMedia.h"
#include "GraphicsFullScreenVideo.h"
#include "MultiMediaDlg.h"


// GraphicsFullScreenVideo dialog
#define MY_TIMEREVENT1 9993

IMPLEMENT_DYNAMIC(GraphicsFullScreenVideo, CDialog)

GraphicsFullScreenVideo::GraphicsFullScreenVideo(CWnd* pParent /*=NULL*/)
	: CDialog(GraphicsFullScreenVideo::IDD, pParent)
{

    m_currentPos = 0;
	m_length = 0;
}

GraphicsFullScreenVideo::~GraphicsFullScreenVideo()
{
}

void GraphicsFullScreenVideo::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(GraphicsFullScreenVideo, CDialog)
	ON_WM_SHOWWINDOW()
	ON_WM_TIMER()
	ON_WM_CLOSE()
	ON_WM_SIZE()
	ON_WM_MOUSEMOVE()
	ON_WM_KEYDOWN()
END_MESSAGE_MAP()


// GraphicsFullScreenVideo message handlers
LONGLONG GraphicsFullScreenVideo::GetSeekPos()
{
	return m_currentPos;
}
BOOL GraphicsFullScreenVideo::DestroyWindow() 
{
	// TODO: Add your specialized code here and/or call the base class
    m_video.pMediaSeeking ->GetCurrentPosition(&m_currentPos);
	m_video.MultiMedia_Clean();
	return CDialog::DestroyWindow();
}

void GraphicsFullScreenVideo::OnShowWindow(BOOL bShow, UINT nStatus)
{
	CDialog::OnShowWindow(bShow, nStatus);

	// TODO: Add your message handler code here

    m_video.SetWindowParent(this);
	m_video.MultiMedia_Initialize(m_path);

	CWnd* desk=GetDesktopWindow();
	RECT re;
	desk->GetClientRect(&re);

	SetTimer(MY_TIMEREVENT1,1000,(TIMERPROC)NULL);// set timer 1s
	::ShowWindow(this->GetSafeHwnd(),SW_MAXIMIZE);

	//
	m_video.BuildFilterGraph(this, re);	

	m_video.pMediaControl->Run();
	m_video.pAudio->put_Volume(m_lVolume);
	m_video.pMediaSeeking->SetPositions(&m_currentPos, AM_SEEKING_AbsolutePositioning,NULL,AM_SEEKING_NoPositioning);
	m_video.pMediaSeeking->GetDuration(&m_length);
	m_video.UpdateVideoWindow(&re);

	m_fullScreentransf= new FullScreenTranparentShow(this);
	m_fullScreentransf->Create(FullScreenTranparentShow::IDD,this);

	m_fullScreentransf->ShowWindow(true);
}

BOOL GraphicsFullScreenVideo::PreTranslateMessage(MSG* pMsg)
{
	// TODO: Add your specialized code here and/or call the base class
	if(pMsg->message== WM_GRAPHNOTIFY)
		HandleEventGraph();

	return CDialog::PreTranslateMessage(pMsg);
}

void GraphicsFullScreenVideo::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: Add your message handler code here and/or call default
	m_video.pMediaSeeking ->GetCurrentPosition(&m_currentPos);
	m_fullScreentransf->setProgressPos(m_currentPos,m_length);
	
	if(m_fullScreentransf->m_bIsDestroy == TRUE)
	{  
		EndDialog(IDD_FULL_VIDEO_SCREEN);
		m_fullScreentransf->m_bIsDestroy  ==FALSE;

	}

	if(m_fullScreentransf->m_bPause == TRUE)
	{
		m_video.MultiMedia_Pause();
	}
	if(m_fullScreentransf->m_bPlaying  ==TRUE)
	{
		m_video.pMediaControl->Run();
	}
	if(m_fullScreentransf->m_bExit == TRUE)
	{
		OnOK();
	}

	CDialog::OnTimer(nIDEvent);
}

void GraphicsFullScreenVideo::OnClose()
{
	// TODO: Add your message handler code here and/or call default
	m_video.pMediaSeeking ->GetCurrentPosition(&m_currentPos);
    OnOK();
	CDialog::OnClose();
}
void GraphicsFullScreenVideo::HandleEventGraph()
{
	//MediaPlay();
	HRESULT hr = S_OK;
	long evCode, param1,param2;
	if(!m_video.pMediaEventEx)
	{
		AfxMessageBox(_T("MediaEventEx interface error"));
	}
	while (SUCCEEDED(m_video.pMediaEventEx->GetEvent(&evCode, &param1, &param2, 0)))
	{
		switch(evCode)
		{
		case EC_OLE_EVENT:
			{
				m_fullScreentransf->SetText((BSTR)param2,true);
			}
			break;
		case EC_COMPLETE:
			if (m_fullScreentransf)
			{
				EndDialog(IDD_FULL_VIDEO_SCREEN);
			}
			break;
		}
		hr = m_video.pMediaEventEx->FreeEventParams(evCode, param1, param2);
	}
}

void GraphicsFullScreenVideo::OnSize(UINT nType, int cx, int cy)
{
	CDialog::OnSize(nType, cx, cy);

	// TODO: Add your message handler code here
}

void GraphicsFullScreenVideo::OnMouseMove(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default

	CDialog::OnMouseMove(nFlags, point);
}

void GraphicsFullScreenVideo::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	// TODO: Add your message handler code here and/or call default
	if (nChar == VK_ESCAPE)
		OnOK();

	CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
}

